cc.Class({
    extends: cc.Component,

    properties: {
        player: {
            type: cc.Node,
            default: null
        },
        dici: {
            type: cc.Prefab,
            default: null
        },
        jumpAudio: {
            type: cc.AudioClip,
            default: null
        },
        wallWidth: 80
    },
    // 点击右边
    toMoveRight: function(e) {
        // player的y坐标: this.player.y
        var goRight = cc.moveTo(0.2, cc.v2(this.node.width/2 - this.wallWidth, this.player.y))
        var goR1 = cc.moveTo(0.1, cc.v2(this.node.width/2 - this.wallWidth - 30, this.player.y))
        var goR2 = cc.moveTo(0.1, cc.v2(this.node.width/2 - this.wallWidth, this.player.y))
        // y轴对称旋转180度
        this.player.eulerAngles = cc.v3(0, 180, 0)
        // 沿y轴旋转的角度 this.player.eulerAngles.y
        if (this.player.eulerAngles.y === 180) {    // player在右
            // 踏步走
            var seq = cc.sequence(goR1, goR2)
            this.player.runAction(seq)
        } else {    // player在左
            this.player.runAction(goRight)
        }
    },
    // 点击左边
    toMoveLeft: function(e) {
        // 小人儿的y坐标: this.player.y
        var goLeft = cc.moveTo(0.2, cc.v2(-this.node.width/2 + this.wallWidth, this.player.y))
        var goL1 = cc.moveTo(0.1, cc.v2(-this.node.width/2 + this.wallWidth + 30, this.player.y))
        var goL2 = cc.moveTo(0.1, cc.v2(-this.node.width/2 + this.wallWidth, this.player.y))
        if (this.player.eulerAngles.y === 0) {  // player在左, 点击位置在左 ==> 踏步
            var seq = cc.sequence(goL1, goL2)
            this.player.runAction(seq)
        } else { // player在右, 点击位置在左 ==> 踏步
            this.player.runAction(goLeft)
        }
        this.player.eulerAngles = cc.v3(0, 0, 0)
    },
    // 注册函数
    setInputControl: function() {
        var _this = this
        // 触摸开始
        this.node.on('touchstart', function(e) {
            var location = e.getLocation()
            // 全局 --转换成--> 相对于this.node
            var locationOfThisNode = _this.node.convertToNodeSpaceAR(location)
            if (locationOfThisNode.x < 0) { // 左
              _this.toMoveLeft()
            } else {   // 右
              _this.toMoveRight()
            }
            // 分数加一
            _this.scoreNum++
            _this.scoreLabel.string = '' + _this.scoreNum
            // 播放音效
            cc.audioEngine.play(_this.jumpAudio, false, 0.2)
            // 将分数存到localStorage中
            cc.sys.localStorage.setItem('score', _this.scoreNum)
            //  移动所有地刺
            _this.moveDici()
            // 生成新的地刺
            _this.newDici()
        }, this)
        // 移动
        this.node.on('touchmove', function(e) {}, this)
        // 结束
        this.node.on('touchend', function(e) {}, this)
        this.node.on('touchcancel', function(e) {}, this)

    },
    // 产生地刺
    newDici: function() {
        // 实例化一个地刺
        var diciIns = cc.instantiate(this.dici)
        // 将地刺放入界面中
        this.node.addChild(diciIns)
        this.diciArr.push(diciIns)
        // 判断位置(左右)
        var randomNum = Math.random()
        if (randomNum >= 0.5) { // 使>=0.5时 地刺生在右边
            // 设置地刺朝向
            // diciIns.eulerAngles = cc.v3(0, 0, 0)
        } else {  // 地刺生在左边
            // 旋转
            diciIns.is3DNode = true
            diciIns.eulerAngles = cc.v3(0, 180, 0)
        }
        // 设置坐标(x, y)
        diciIns.setPosition(this.setPosition(randomNum))
    },
    setPosition: function(randomNum) {
        var dX = 0
        var dY = 0
        // X
        if (randomNum >= 0.5) {
            dX = this.node.width / 2 - this.wallWidth
        } else {
            dX = -this.node.width / 2 + this.wallWidth
        }
        // Y
        if(this.diciCount < 8) {    // 初始生成的八个地刺
            dY = this.node.height / 2 - this.diciDis * this.diciCount - this.diciDis*2
        } else {  // 每次点击生成的地刺放在第八个地刺位置
            dY = this.node.height / 2 - this.diciDis * 8 - this.diciDis*2
        }
        // cc.log(randomNum, 'randomNum')
        return cc.v2(dX, dY)
    },
    moveDici: function() {
        for (var i = 0; i < this.diciArr.length; i++) {
            var goAhead = cc.moveBy(0.2, cc.v2(0, 140))
            this.diciArr[i].runAction(goAhead)
        }
    },
    // LIFE-CYCLE CALLBACKS:
    startGame: function() {
        // 初始化地刺
        this.diciArr = []
        for (var i = 0; i < 8; i++) {
            this.diciCount = i
            this.newDici()
        }
        // 初始化倒计时和分数
        this.timeNum = 60
        this.scoreNum = 0
        this.timeLabel.string = '' + this.timeNum
        this.scoreLabel.string = '' + this.scoreNum
        this.schedule(this.countDown, 1)
    },
    // 倒计时
    countDown: function() {
        this.timeNum--
        this.timeLabel.string = '' + this.timeNum
        if (this.timeNum <= 0) { // 时间到
            // 停止计时器
            this.unscheduleAllCallbacks()
            // 游戏结束界面
            cc.director.loadScene('OverScene')
            return
        }
    },
    onLoad: function() {
        this.setInputControl()
        this.player.is3DNode = true
        this.diciDis = 140  // 地刺间隔距离
        this.player.x = -this.node.width/2 + this.wallWidth
        this.player.y = this.node.height/2 - this.diciDis
        // 预加载场景
        cc.director.preloadScene('OverScene')
        this.timeLabel = cc.find('Home/timer/time').getComponent(cc.Label)
        this.scoreLabel = cc.find('Home/score').getComponent(cc.Label)
        this.startGame()
    }

    // update (dt) {},
});
